package com.dmd.yikang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dmd.yikang.common.CustomException;
import com.dmd.yikang.dto.MedicineDto;
import com.dmd.yikang.entity.Medicine;
import com.dmd.yikang.entity.MedicineType;
import com.dmd.yikang.mapper.MedicineMapper;
import com.dmd.yikang.service.MedicineService;
import com.dmd.yikang.service.MedicineTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author daimingdong
 * @version 1.0.0
 * create 2024/5/27 18:19
 * description
 * @title MedicineServiceImpl
 **/
@Service
@Slf4j
public class MedicineServiceImpl extends ServiceImpl<MedicineMapper, Medicine> implements MedicineService {

    @Autowired
    private MedicineTypeService medicineTypeService;

    /**
     * 保存药品信息和药品类型信息
     * @param medicineDto
     */
    @Transactional
    public void saveWithType(MedicineDto medicineDto) {
        //保存药品基本信息到药品表
        this.save(medicineDto);

        Long medicineId = medicineDto.getId();//药品Id

        //药品类型
        List<MedicineType> flavors = medicineDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setMedicineId(medicineId);
            return item;
        }).collect(Collectors.toList());

        //保存药品类型信息到药品类型表
        medicineTypeService.saveBatch(flavors);
    }


    /**
     * 根据药品Id查询药品信息和药品类型信息
     * @param id
     * @return
     */
    public MedicineDto getByIdWithType(Long id) {
        //查询药品信息
        Medicine medicine = this.getById(id);

        MedicineDto medicineDto = new MedicineDto();

        BeanUtils.copyProperties(medicine,medicineDto);

        //查询药品类型信息
        LambdaQueryWrapper<MedicineType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MedicineType::getMedicineId,medicine.getId());
        List<MedicineType> Type = medicineTypeService.list(queryWrapper);
        medicineDto.setFlavors(Type);

        return medicineDto;
    }

    /**
     * 修改药品信息和药品类型信息
     * @param medicineDto
     */
    @Override
    @Transactional
    public void updateWithType(MedicineDto medicineDto) {
        //更新medicine表
        this.updateById(medicineDto);

        //清理当前药品对应的药品类型数据
        LambdaQueryWrapper<MedicineType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MedicineType::getMedicineId,medicineDto.getId());

        medicineTypeService.remove(queryWrapper);

        //添加当前提交的药品数据
        List<MedicineType> flavors = medicineDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setMedicineId(medicineDto.getId());
            return item;
        }).collect(Collectors.toList());

        medicineTypeService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public void deleteById(List<Long> ids) {

        //构造条件查询器
        LambdaQueryWrapper<Medicine> queryWrapper = new LambdaQueryWrapper<>();

        //先查询该药品是否在售卖，如果是则抛出异常
        queryWrapper.in(ids != null,Medicine::getId,ids);
        List<Medicine> list = this.list(queryWrapper);
        for (Medicine medicine : list){
            Integer status = medicine.getStatus();
            //如果药品状态为0，则可以删除
            if (status == 0){
                this.removeById(medicine.getId());
            }else {
                throw new CustomException("该药品正在售卖中，不能删除");
            }
        }
    }
}
